import 'package:flutter/material.dart';
import './TabPage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '状态保持',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: KeepState(),
    );
  }
}

class KeepState extends StatefulWidget {
  const KeepState({Key? key}) : super(key: key);

  @override
  _KeepStateState createState() => _KeepStateState();
}

// SingleTickerProviderStateMixin 主要是我们初始化TabController时，
// 需要用到vsync ，垂直属性，然后传递this
class _KeepStateState extends State<KeepState> with SingleTickerProviderStateMixin {
  late TabController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = TabController(length: 3, vsync: this);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('状态保持'),
        bottom: TabBar(
          controller: _controller,
          tabs: [
            Tab(icon: Icon(Icons.directions_car)),
            Tab(icon: Icon(Icons.directions_transit)),
            Tab(icon: Icon(Icons.directions_bike)),
          ]
        ),
      ),
      body: TabBarView(
        controller: _controller,
        children: [
          TabPage(title: '汽车',),
          TabPage(title: '大巴',),
          TabPage(title: '自行车',),
        ],
      )
    );
  }
}

